#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <string>

#include "nlohmann/json.hpp"
#include "utils/strategy_base.h"
#include <sstream>

using json = nlohmann::json;
// 具体的策略类
class StrategyExample : public BaseStrategy {
public:
  explicit StrategyExample(const std::string &config_path) {
    // 从配置文件加载参数
    std::ifstream config_file(config_path);
    if (config_file.is_open()) {
      std::stringstream buffer;
      buffer << config_file.rdbuf();
      config_ = buffer.str();
      config_file.close();
    } else {
      std::cerr << "Failed to open config file: " << config_path << std::endl;
    }
  }

  void OnTick(const Tick &tick) override {
    // 获取当前时间
    auto now = std::chrono::system_clock::now();
    std::time_t now_time = std::chrono::system_clock::to_time_t(now);
    char time_str[30];
    std::strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S",
                  std::localtime(&now_time));
    json config_json;
    try {
      config_json = json::parse(config_);
      std::string name = config_json["name"];
      // 输出 symbol 和 name
      std::cout << time_str << "| Symbol: " << tick.symbol << "| Name: " << name
                << std::endl;
    } catch (const json::parse_error &e) {
      std::cerr << "Failed to parse config: " << e.what() << std::endl;
    }
  }

private:
  std::string config_;
};

// 策略工厂实现
class StrategyFactoryExample : public StrategyFactory {
public:
  BaseStrategy *CreateStrategy(const std::string &config_path) override {
    return new StrategyExample(config_path);
  }
};

// 导出工厂创建函数
extern "C" StrategyFactory *CreateFactory() {
  return new StrategyFactoryExample();
}